package com.zxx.cascommon.base.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * 日期时间工具类
 *
 * @author zhoukai
 * @date 2020/3/12 15:11
 */
public class LocalDateTimeUtils {

    /**
     * 根据传入的localDate转Date
     *
     * @author zhoukai
     * @date 2019/12/23 18:38
     */
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
    }

    /**
     * 根据传入的localDateTime转Date
     *
     * @author zhoukai
     * @date 2019/12/23 18:38
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
    }

    /**
     * 获取传入时间的毫秒数
     *
     * @author zhoukai
     * @date 2019/11/19 15:55
     */
    public static long getMilli(LocalDateTime timestamp) {
        return timestamp.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 获取传入时间的秒数
     *
     * @author zhoukai
     * @date 2019/11/19 15:55
     */
    public static long getSecond(LocalDateTime timestamp) {
        return timestamp.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 时间戳转化成localDateTime类型
     *
     * @author zhoukai
     * @date 2019/11/19 15:11
     */
    public static LocalDateTime longToLocalDateTime(Long timestamp) {
        return LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8));
    }

    /**
     * 时间戳转化成字符串类型
     *
     * @author zhoukai
     * @date 2019/11/19 15:11
     */
    public static String longToString(Long timestamp) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8));
        return localDateTime.format(dtf);
    }

    /**
     * 时间戳转化成date类型
     *
     * @author zhoukai
     * @date 2019/11/19 15:11
     */
    public static Date longToDate(Long timestamp) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8));
        return localDateTimeToDate(localDateTime);
    }

    /**
     * 获取当前月份
     *
     * @author zhoukai
     * @date 2019/11/19 15:11
     */
    public static int getMonthValue(LocalDateTime timestamp) {
        return timestamp.getMonthValue();
    }

    /**
     * 获取当前年份
     *
     * @author zhoukai
     * @date 2019/11/19 15:11
     */
    public static int getYearValue(LocalDateTime timestamp) {
        return timestamp.getYear();
    }

    /**
     * 获取指定时间本月第一天
     *
     * @author zhoukai
     * @date 2019/11/19 15:11
     */
    public static LocalDateTime firstDayOfMonths(LocalDateTime timestamp) {
        return timestamp.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
    }

    /**
     * 获取指定时间本月最后一天
     *
     * @author zhoukai
     * @date 2019/11/19 15:11
     */
    public static LocalDateTime lastDayOfMonths(LocalDateTime timestamp) {
        return timestamp.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
    }

    /**
     * 获取两个日期的差  field参数为ChronoUnit.*
     *
     * @param field 单位(年月日时分秒)
     * @return 相差的值
     */
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) {
            return period.getYears();
        }
        if (field == ChronoUnit.MONTHS) {
            return period.getYears() * 12 + period.getMonths();
        }
        return field.between(startTime, endTime);
    }

    /**
     * 计算两个时间相差的月份
     *
     * @author zhoukai
     * @date 2019/11/27 09:55
     */
    public static long betweenTwoMonth(LocalDateTime startTime, LocalDateTime endTime) {
        int startYear = getYearValue(startTime);
        int endYear = getYearValue(endTime);
        int startMonth = getMonthValue(startTime);
        int endMonth = getMonthValue(endTime);
        return (endYear - startYear) * 12 + (endMonth - startMonth);
    }

    /**
     * 根据传入的时间戳（毫秒级)以及需要增加的时间，返回处理之后的时间
     *
     * @param expireTime 分钟
     * @author zhoukai
     * @date 2020/7/2 17:30
     */
    public static Date addMinutes(long curTime, int expireTime) {
        LocalDateTime localDateTime = longToLocalDateTime(curTime);
        localDateTime = localDateTime.plus(expireTime, ChronoUnit.MINUTES);
        return localDateTimeToDate(localDateTime);
    }

    public static void main(String[] args) {
        LocalDateTime start = longToLocalDateTime(1543593600000L);
        LocalDateTime end = longToLocalDateTime(1574760662000L);
        System.out.println(start);
        System.out.println(end);
        System.out.println("月:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MONTHS));
        System.out.println("月:" + LocalDateTimeUtils.betweenTwoMonth(start, end));
        System.out.println(firstDayOfMonths(start));
        System.out.println(lastDayOfMonths(start));
        System.out.println(getMonthValue(start));
        System.out.println(getYearValue(start));
        System.out.println(getMilli(firstDayOfMonths(start)));
        System.out.println(getMilli(lastDayOfMonths(start)));
        System.out.println(Instant.now().getEpochSecond());
        DateTimeFormatter todf = DateTimeFormatter.ofPattern("yyyyMMdd");
        String str = "20171121";
        LocalDate time = LocalDate.parse(str, todf);
        System.out.println(time.toString());
    }

}
